<template>
  <div :data-tenantId="tenantId" :data-envir="envir" :data-state="state" :data-acr_values="acr_values">
    <oidc-register v-if="envir === 'game'" />
    <game-register v-if="envir === 'platform'" />
    <mobile v-if="envir === 'mobile'" />
  </div>
</template>

<script>
import oidcRegister from '@/views/register/oidcRegister.vue';
import gameRegister from '@/views/register/gameRegister.vue';
import mobile from '@/views/register/mobile.vue';
import { setCookieTenant } from '@/lib/auth';
export default {
  components: {
    oidcRegister,
    gameRegister,
    mobile
  },
  data() {
    return {
      envir: '',
      tenantId: '',
      state: '',
      acr_values: ''
    };
  },
  mounted() {
    let returnUrlData = '';
    if (this.$route.query && this.$route.query.ReturnUrl) {
      returnUrlData = decodeURIComponent(decodeURIComponent(this.$route.query.ReturnUrl)).split('&');
    }
    console.log(returnUrlData);
    let tenantId = '';
    let hasState = false;
    for (const i in returnUrlData) {
      if (/^acr_values=/.test(returnUrlData[i])) {
        tenantId = returnUrlData[i].replace('acr_values=', '');
        this.acr_values = tenantId;
        // break;
      }
      if (/^state=/.test(returnUrlData[i])) {
        this.state = returnUrlData[i].replace('state=', '');
        hasState = true;
      }
    }
    if (this.$route.path === '/' && !hasState) {
      this.$message({ type: 'error', message: '缺少返回地址！' });
      this.$router.push({ path: '/404', query: this.$route.query });
      return false;
    }
    if (!hasState) {
      this.$message({ type: 'error', message: '缺少返回地址！' });
      this.$router.push({ path: '/404' });
      return false;
    }
    this.tenantId = tenantId ? window.atob(tenantId) : '';
    this.envir =
      this.tenantId && this.tenantId.split('___').length > 1
        ? this.tenantId.split('___')[1]
        : 'game';
    this.tenantId =
      this.tenantId && this.tenantId.split('___').length > 0
        ? this.tenantId.split('___')[0]
        : '';
    if (navigator.userAgent.match(/(phone|iPhone|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
      this.envir = 'mobile';
    }
    console.log('envir-----------:', this.envir);
    console.log('tenantId-----------:', this.tenantId);
    this.tenantId = this.tenantId === '%2C' ? '' : this.tenantId;
    const _host = window.location.hostname;
    if (!this.tenantId && _host !== 'tfs.ydfuture.com:44321') {
      switch (_host) {
        case 'auth-test.wtwuxicenter.com':
          this.tenantId = '39fd9624-25ad-b0a6-8ab9-b819f2fa3ddb';
          break;
        case 'auth2.wtwuxicenter.com':
          this.tenantId = '39fb367b-579a-4662-7a86-5d149ca9da3a';
          break;
        case 'auth.wtwuxicenter.com':
          this.tenantId = '39fb367b-579a-4662-7a86-5d149ca9da3a';
          break;
        default:
          this.tenantId = '39fa5ab5-a249-ee6b-e05b-346116999b43';
      }
      console.log('设置默认token', this.tenantId);
    }
    if (!this.tenantId) {
      this.$message({ type: 'error', message: 'tenantId不可用！' });
      const _query = { msg: '平台', ...this.$route.query };
      this.$router.push({ path: '/404', query: _query });
      return false;
    }
    setCookieTenant(this.tenantId);
  }
};
</script>

<style scoped>
</style>
